package com.threeox.commonlibrary.entity.engine.request.network;

import com.alibaba.fastjson.JSONObject;
import com.threeox.commonlibrary.entity.engine.cache.CacheConfigMsg;
import com.threeox.commonlibrary.entity.engine.request.base.BaseRequestMsg;
import com.threeox.commonlibrary.util.RegexHelper;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.res.StringHelper;

/**
 * 
 * @ClassName: NetWorkRequestMsg
 * 
 * @Description: TODO(网络请求的对象)
 * 
 * @author 赵屈犇
 * 
 * @date 创建时间: 2017/7/4 12:21
 * 
 * @version 1.0
 * 
 */
public class NetWorkRequestMsg extends BaseRequestMsg {

    private Long readTimeOut = 30000L; //默认为30000  30秒
    private Long writeTimeOut = 30000L;//默认为30000  30秒
    private Long connTimeOut = 30000L;//默认为30000  30秒

    private String rootUrl; // 根目录请求地址 全部规定使用定义在string文件中
    private String serveUrl;// 请求的地址
    private String methodType;// 请求方法的类型 默认为GET请求类型
    private JSONObject headers; // 请求的头部对象

    private Class responseClass; //响应结果数据的Class,默认是JSONObject
    private String responseClassName; //响应结果数据的Class全名称,默认是JSONObject
    private ResponseConfigMsg respConfigMsg; //响应数据解析对象配置

    private CacheConfigMsg cacheConfig;// 缓存配置对象

    private JSONObject headersKeys; // 请求的头部对象可替换的主键

    public void initData(StringHelper stringHelper) {
        this.rootUrl = stringHelper.getStringText(rootUrl);
        this.serveUrl = stringHelper.getStringText(serveUrl);
        super.initData(stringHelper, serveUrl);
        this.headers = RegexHelper.replaceSys(headers);
        if (null != respConfigMsg) {
            respConfigMsg.initData(stringHelper);
        }
        try {
            if (EmptyUtils.isNotEmpty(this.responseClassName)) {
                this.responseClassName = stringHelper.getString(responseClassName);
                this.responseClass = Class.forName(responseClassName);
            } else {
                this.responseClass = JSONObject.class;
            }
            this.headersKeys = RegexHelper.getReplaceKeys(headers);
        } catch (Exception e) {
            this.responseClass = JSONObject.class;
        }
    }

    public Long getReadTimeOut() {
        return readTimeOut;
    }

    public void setReadTimeOut(Long readTimeOut) {
        this.readTimeOut = readTimeOut;
    }

    public Long getWriteTimeOut() {
        return writeTimeOut;
    }

    public void setWriteTimeOut(Long writeTimeOut) {
        this.writeTimeOut = writeTimeOut;
    }

    public Long getConnTimeOut() {
        return connTimeOut;
    }

    public void setConnTimeOut(Long connTimeOut) {
        this.connTimeOut = connTimeOut;
    }

    public String getRootUrl() {
        return rootUrl;
    }

    public void setRootUrl(String rootUrl) {
        this.rootUrl = rootUrl;
    }

    public String getServeUrl() {
        return serveUrl;
    }

    public void setServeUrl(String serveUrl) {
        this.serveUrl = serveUrl;
    }

    public String getMethodType() {
        return methodType;
    }

    public void setMethodType(String methodType) {
        this.methodType = methodType;
    }

    public JSONObject getHeaders() {
        return headers;
    }

    public void setHeaders(JSONObject headers) {
        this.headers = headers;
    }

    public Class getResponseClass() {
        return responseClass;
    }

    public void setResponseClass(Class responseClass) {
        this.responseClass = responseClass;
    }

    public String getResponseClassName() {
        return responseClassName;
    }

    public void setResponseClassName(String responseClassName) {
        this.responseClassName = responseClassName;
    }

    public ResponseConfigMsg getRespConfigMsg() {
        return respConfigMsg;
    }

    public void setRespConfigMsg(ResponseConfigMsg respConfigMsg) {
        this.respConfigMsg = respConfigMsg;
    }

    public CacheConfigMsg getCacheConfig() {
        return cacheConfig;
    }

    public void setCacheConfig(CacheConfigMsg cacheConfig) {
        this.cacheConfig = cacheConfig;
    }

    /**
     * public void initRequestHeaders(Intent intent) {
     * RegexHelper.replace(this.headers, headersKeys, intent);
     * }
     */

    public void initRequestHeaders(Object data) {
        RegexHelper.replace(this.headers, headersKeys, data);
    }
}
